﻿using MD5Hash;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class LoginDal
    {
        public LoginDal(MyContext context)
        {
            Context = context;
        }

        public MyContext Context { get; }


        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="l">属性</param>
        /// <returns></returns>
        public int AddLogin(Login l)
        {
            try
            {
                l.Pwd = l.Pwd.GetMD5();
                Context.Login.Add(l);
                return Context.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="Name">名字</param>
        /// <param name="Pwd">密码</param>
        /// <returns></returns>
        public dynamic LoginSelect(string Name, string Pwd)
        {
            try
            {
                Pwd = Pwd.GetMD5();
                var res = Context.Login.Where(x => x.Name == Name).Count();
                if (res == 0)
                {
                    return -1;
                }
                else
                {

                    var ress = Context.Login.Where(x => x.Name == Name && x.Pwd == Pwd).Count();
                    if (ress == 0)
                    {
                        return -2;
                    }
                    else
                    {

                        var resss = Context.Login.Where(x => x.Name == Name && x.Pwd == Pwd).FirstOrDefault();
                        return resss;
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
